/***************************************************************************
/
/ the PartnerController class manages all the user actions related
/ to viewing Partner content, includeing the redirection to a
/ Partner site when the related link is clicked on
/
/**************************************************************************/

class PartnerController {


	//define the default redirection for the PartnerController
    def index = { redirect( action : list, params : params ) }


    // the delete, save and update actions only accept POST requests
    def allowedMethods = [ delete : 'POST', save : 'POST', update : 'POST' ]


    // show the list of partner organization with a picture and link to the partner organizations' website
    def list = {

        [ partnerList : Partner.list( params ) ]

    }


    // this method will keep information of the user who requests to go to the partner organizations' website
    def show = {

		def referralLog = new ReferralLog()

		// add the user information (partner, client ipAddress)
		// if adding is not success, go to error page
		if ( referralLog ) {

			referralLog.ipAddress = request.remoteAddr
			referralLog.partner = Partner.get( params.id )

			// store the data into the database
			referralLog.save()

			// if there is no error, the new windows will be popup and redirect to the partner's website
			if ( !referralLog.hasErrors() ) {

				redirect( url : "http://${referralLog.partner.url}" )

			}

		}

		// Code reaching here means there is an error.
		// It will show an error message on the page
		flash.message = "Can't find the partner website."

    }

}